package com.dongdongshop.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.mapper.TbGoodsDescMapper;
import com.dongdongshop.mapper.TbGoodsMapper;
import com.dongdongshop.pojo.TbGoods;
import com.dongdongshop.pojo.TbGoodsDesc;
import com.dongdongshop.service.GoodsService;
import com.dongdongshop.vo.CategoryVO;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@DubboService
public class GoodsServiceImpl implements GoodsService {

    @Resource
    private TbGoodsMapper goodsMapper;

    @Resource
    private TbGoodsDescMapper goodsDescMapper;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Override
    public Long addGoodsByGoodsDesc(TbGoods goods, TbGoodsDesc goodsDesc) {
        int insert = goodsMapper.insert(goods);
        if (insert != 0){
            goodsDesc.setGoodsId(goods.getId());
            goodsDescMapper.insert(goodsDesc);
        }
        return goods.getId();
    }

    @Override
    public List<CategoryVO> goodsInfo() {
        return goodsMapper.goodsInfo();
    }

    @Override
    public List<TbGoods> getGoodsInfo() {
        return goodsMapper.selectByExample(null);
    }

    @Override
    public int deleteBatch(Long[] ids) {
        goodsDescMapper.deleteBatch(ids);
        return goodsMapper.deleteBatch(ids);
    }

    @Override
    public int updateBatch(Long[] ids, String auditStatus) {
        if (auditStatus.equals("1")){
            String s = JSONObject.toJSONString(ids);
            rocketMQTemplate.convertAndSend("dongdongshop-topic",s);
        }
        return goodsMapper.updateBatch(ids, auditStatus);
    }

    @Override
    public TbGoods selectByGoodsId(Long goodsId) {
        return goodsMapper.selectByPrimaryKey(goodsId);
    }

    @Override
    public TbGoodsDesc getGoodsDescByGoodsId(Long goodsId) {
        return goodsDescMapper.selectByPrimaryKey(goodsId);
    }
}
